import Taro, {useMemo} from '@tarojs/taro'
import {DelayQuerySelector, IsNullObject} from './utils';

const taroUtils = {}

  /**
   * 获取系统信息
   * { screenWidth: 750, ... }
   */
  taroUtils.GetSystemInfo=()=>{
    return Taro.getSystemInfoSync()
  }
  /**
   * 获取当前帐号信息。
   * {
   *    miniProgram: {
   *      appId: '小程序 appid'
   *    },
   *   plugin: {
   *      appId: '插件 appid',
   *     version: '插件版本号'
   *    }
   * /}
   */
  taroUtils.GetAccountInfo=()=>{
    return  Taro.getAccountInfoSync()
  }

  /**
   * 获取小程序启动时的参数。
   * { path: '启动小程序的路径', ... }
   */
  taroUtils.GetLaunchOptions=()=>{
    return Taro.getLaunchOptionsSync()
  }
  /**
   * 获取菜单按钮（右上角胶囊按钮）的布局位置信息。坐标信息以屏幕左上角为原点。
   * { width: 333, ... }
   */
  taroUtils.GetMenuButtonBoundingClientRect=()=>{
    return  Taro.getMenuButtonBoundingClientRect()
  }

  /**
   *获取当前页url
   * pages/index/index
   */
  taroUtils.GetCurrentPageUrl=()=>{
    let pages = getCurrentPages()
    let currentPage = pages[pages.length - 1]
    let url = currentPage.route
    return url
  }
/**
 * 获取滚动条剩余高度
 * */
taroUtils.GetScrollViewSurplusContain=(self,className)=>{
  return new Promise(resolve => {
    DelayQuerySelector(self,className).then((res)=>{
      const info = Taro.getSystemInfoSync()
      let { windowHeight } = info
      if(windowHeight <=0) windowHeight = 450
      if (IsNullObject(res)){
        resolve(windowHeight)
      }else {
        if (res.length > 0) {

          const  height = windowHeight - res[0].height - res[0].top
          resolve(height)
        }else{
          resolve(windowHeight)
        }
      }
    })
  })
}

export default taroUtils
